package com.dididoctor.patient.Activity.Usercentre.Record.Cases.CasesDetail;

import android.content.Intent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dididoctor.patient.Activity.Evaluate.EvaluateActivity;
import com.dididoctor.patient.MV.EduActivity;
import com.dididoctor.patient.R;
import com.dididoctor.patient.Ui.ItemListView;
import com.dididoctor.patient.Utils.Util;

import java.util.ArrayList;
import java.util.List;

/**
 * @version 2.0
 * @类描述：
 * @项目名称：ddgh
 * @类名称： CasesDetailActivity
 * @包名称： com.dididoctor.patient.Activity.Usercentre.Record.Cases.CasesDetail
 * @创建人： zhanjunjie
 * @创建时间：2016/4/15 10:46
 * @修改人：
 * @修改时间：2016/4/15 10:46
 * @修改备注： new parent class(用一句话描述修改了什么)
 */
public class CasesDetailActivity extends EduActivity implements CasesDetailView {

    private TextView mTvHospital, mTvdate, mTvName, mTvJibing, mTvResult, mTvEvaluate, mTvContext;
    private ItemListView listview;
    private RelativeLayout mRldialog;
    private CasesDetailPresenter presenter;

    //医生建议
    private TextView mTvSurvey, mTvMedicine, mTvSee, mTvOther;
    private LinearLayout mLlSurvey, mLlMedicine, mLlSee, mLlOther, mLlEvaluate, mLlInquirty;

    private CasesDetailDialogAdapter dialogadapter;
    private ListView mLvDialog;
    private List<Quslist> quslists = new ArrayList<>();

    private String recId = "";
    private String type = "";

    //评价图片
    private CheckBox cbone, cbtwo, cbthree, cbfour, cbfive;
    private CheckBox[] checkBoxes;

    public static final int TO_EVLAU = 1003;
    public String iscome = "0";

    @Override
    public void init() {
        initview();

        initdata();
    }

    private void initdata() {

        dialogadapter = new CasesDetailDialogAdapter(this, quslists);
        mLvDialog.setAdapter(dialogadapter);


        presenter = new CasesDetailPresenter(this, this);
        presenter.getcasesdetail(recId);
    }

    private void initview() {

        recId = getIntent().getStringExtra("recId");
        type = getIntent().getStringExtra("type");
        //从联通界面进来会传入iscome = “1”，来区分这是诊断书，其他地方进入的都是病历详情
        iscome = getIntent().getStringExtra("iscome");
        if ("1".equals(iscome)) {
            setText(R.id.tv_title, "诊断书");
        } else {
            setText(R.id.tv_title, "病历详情");
        }
        mTvHospital = (TextView) findViewById(R.id.tv_hospital);
        mTvdate = (TextView) findViewById(R.id.tv_date_time);
        mTvJibing = (TextView) findViewById(R.id.tv_jibing);
        mTvName = (TextView) findViewById(R.id.tv_doctorname);
        mTvResult = (TextView) findViewById(R.id.tv_result);
        mTvEvaluate = (TextView) findViewById(R.id.tv_evaluate);
        mTvContext = (TextView) findViewById(R.id.tv_context);
        mRldialog = (RelativeLayout) findViewById(R.id.rl_dialog);
        listview = (ItemListView) findViewById(R.id.listview);
        mLvDialog = (ListView) findViewById(R.id.dialog_listview);
        mLlEvaluate = (LinearLayout) findViewById(R.id.ll_evluate);
        mLlInquirty = (LinearLayout) findViewById(R.id.ll_inquiry);
        if ("3".equals(type)) {
            mLlInquirty.setVisibility(View.GONE);
        } else {
            mLlInquirty.setVisibility(View.VISIBLE);
        }

        //医生建议
        mTvSurvey = (TextView) findViewById(R.id.tv_survey);
        mTvMedicine = (TextView) findViewById(R.id.tv_medicine);
        mTvSee = (TextView) findViewById(R.id.tv_see);
        mTvOther = (TextView) findViewById(R.id.tv_other);
        mLlSurvey = (LinearLayout) findViewById(R.id.ll_survey);
        mLlMedicine = (LinearLayout) findViewById(R.id.ll_medicine);
        mLlSee = (LinearLayout) findViewById(R.id.ll_see);
        mLlOther = (LinearLayout) findViewById(R.id.ll_other);
        //评价
        cbone = (CheckBox) findViewById(R.id.cb_one);
        cbtwo = (CheckBox) findViewById(R.id.cb_two);
        cbthree = (CheckBox) findViewById(R.id.cb_three);
        cbfour = (CheckBox) findViewById(R.id.cb_four);
        cbfive = (CheckBox) findViewById(R.id.cb_five);
        checkBoxes = new CheckBox[]{cbone, cbtwo, cbthree, cbfour, cbfive};
        //点击事件
        findViewById(R.id.ll_inquiry).setOnClickListener(this);
        findViewById(R.id.img_cancel).setOnClickListener(this);
        mRldialog.setOnClickListener(this);
    }

    @Override
    public void onClickEvent(View view) {
        switch (view.getId()) {
            case R.id.ll_inquiry:
                mRldialog.setVisibility(View.VISIBLE);
                break;
            case R.id.img_cancel:
                mRldialog.setVisibility(View.GONE);
                break;
            case R.id.rl_dialog:
                break;
        }
    }

    @Override
    public void setContentLayout() {
        setContentView(R.layout.activity_case_detail);
    }

    @Override
    public void getcasesdetail(CasesDetail casesDetail, List<Quslist> quslists, List<Advice> advices, Impress impress) {
        if (casesDetail != null) {
            mTvHospital.setText(casesDetail.getHosName());
            mTvdate.setText(casesDetail.getClinicDate());
            mTvName.setText(casesDetail.getDctName());
            mTvJibing.setText(casesDetail.getDescr());
            mTvResult.setText(casesDetail.getResult());
        }
        if (!Util.isEmpty(quslists)) {
            this.quslists.clear();
            this.quslists = quslists;
            dialogadapter.addData(this.quslists);
            dialogadapter.notifyDataSetChanged();
        }
        if (!Util.isEmpty(advices)) {
            for (int i = 0; i < advices.size(); i++) {
                Advice advice = advices.get(i);
                if ("1".equals(advice.getType())) {
                    mLlSurvey.setVisibility(View.VISIBLE);
                    mTvSurvey.setText(advice.getContent());
                } else if ("2".equals(advice.getType())) {
                    mLlSee.setVisibility(View.VISIBLE);
                    mTvSee.setText(advice.getContent());
                } else if ("3".equals(advice.getType())) {
                    mLlMedicine.setVisibility(View.VISIBLE);
                    mTvMedicine.setText(advice.getContent());
                } else if ("4".equals(advice.getType())) {
                    mLlOther.setVisibility(View.VISIBLE);
                    mTvOther.setText(advice.getContent());
                }
            }
        }

        if (Util.isEmpty(impress.getRemark())) {
            if (!"1".equals(iscome)) {
                mLlEvaluate.setVisibility(View.GONE);
                Intent intent = new Intent(this, EvaluateActivity.class);
                intent.putExtra("recId", recId);
                intent.putExtra("hosId", casesDetail.getHosId());
                startActivityForResult(intent, TO_EVLAU);
            }else{
                mLlEvaluate.setVisibility(View.GONE);
            }
        } else {
            mLlEvaluate.setVisibility(View.VISIBLE);
            mTvContext.setText(impress.getRemark());
            if (Util.toInteger(impress.getPoint()) == 5) {
                mTvEvaluate.setText("满意");
            } else if (Util.toInteger(impress.getPoint()) == 4) {
                mTvEvaluate.setText("一般");
            } else {
                mTvEvaluate.setText("不满意");
            }
            for (int j = 0; j < Util.toInteger(impress.getPoint()); j++) {
                checkBoxes[j].setVisibility(View.VISIBLE);
            }
        }

    }


    @Override
    public void getcasesdetailfail() {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == TO_EVLAU && resultCode == RESULT_OK) {
            presenter.getcasesdetail(recId);
        }
    }
}
